package co.yixiang.yshop.module.store.controller.admin.storewithdrawal;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.store.controller.admin.storewithdrawal.vo.*;
import co.yixiang.yshop.module.store.dal.dataobject.storewithdrawal.StoreWithdrawalDO;
import co.yixiang.yshop.module.store.convert.storewithdrawal.StoreWithdrawalConvert;
import co.yixiang.yshop.module.store.service.storewithdrawal.StoreWithdrawalService;

@Tag(name = "管理后台 - 提现管理")
@RestController
@RequestMapping("/store/withdrawal")
@Validated
public class StoreWithdrawalController {

    @Resource
    private StoreWithdrawalService withdrawalService;

    @PostMapping("/create")
    @Operation(summary = "创建提现管理")
    @PreAuthorize("@ss.hasPermission('store:withdrawal:create')")
    public CommonResult<Long> createWithdrawal(@Valid @RequestBody StoreWithdrawalCreateReqVO createReqVO) {
        return success(withdrawalService.createWithdrawal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提现管理")
    @PreAuthorize("@ss.hasPermission('store:withdrawal:update')")
    public CommonResult<Boolean> updateWithdrawal(@Valid @RequestBody StoreWithdrawalUpdateReqVO updateReqVO) {
        withdrawalService.updateWithdrawal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('store:withdrawal:delete')")
    public CommonResult<Boolean> deleteWithdrawal(@RequestParam("id") Long id) {
        withdrawalService.deleteWithdrawal(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('store:withdrawal:query')")
    public CommonResult<StoreWithdrawalRespVO> getWithdrawal(@RequestParam("id") Long id) {
        return success(withdrawalService.getWithdrawal(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得提现管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('store:withdrawal:query')")
    public CommonResult<List<StoreWithdrawalRespVO>> getWithdrawalList(@RequestParam("ids") Collection<Long> ids) {
        List<StoreWithdrawalDO> list = withdrawalService.getWithdrawalList(ids);
        return success(StoreWithdrawalConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提现管理分页")
    @PreAuthorize("@ss.hasPermission('store:withdrawal:query')")
    public CommonResult<PageResult<StoreWithdrawalRespVO>> getWithdrawalPage(@Valid StoreWithdrawalPageReqVO pageVO) {
        return success(withdrawalService.getWithdrawalPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提现管理 Excel")
    @PreAuthorize("@ss.hasPermission('store:withdrawal:export')")
    @OperateLog(type = EXPORT)
    public void exportWithdrawalExcel(@Valid StoreWithdrawalExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StoreWithdrawalDO> list = withdrawalService.getWithdrawalList(exportReqVO);
        // 导出 Excel
        List<StoreWithdrawalExcelVO> datas = StoreWithdrawalConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "提现管理.xls", "数据", StoreWithdrawalExcelVO.class, datas);
    }

}
